package org.ishequ.weibo.db;

import org.ishequ.weibo.domain.UserInfo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBOpenHelper extends SQLiteOpenHelper {

	// 用来保存UserID、Access Token、Access Secret的表名
	public static final String[] TB_NAME = new String[]{"users","Friend_TimeLine"};
	public static final String[] TABLE_CREATE_SQL = new String[] {
		//user
		"CREATE TABLE IF NOT EXISTS " + TB_NAME[0] + "(" + UserInfo.ID
		+ " integer primary key," + UserInfo.USERID + " varchar,"
		+ UserInfo.TOKEN + " varchar," + UserInfo.TOKENSECRET
		+ " varchar," + UserInfo.USERNAME + " varchar,"
		+ UserInfo.USERICON + " blob" + ");",
		//friendtimeline
		"CREATE TABLE Friend_TimeLine (_id text, screenName text, "
		+ "timelineText text);",
		"CREATE TABLE User_Token (userID long primary key," + "token text,"
		+ "tokenSecret text," + "screenName text);",
		"CREATE TABLE User_Info (userID long primary key," + "name text,"
		+ "screenName text," + "location text,"
		+ "description text," + "url text);"
	};

	public DBOpenHelper(Context context) {
		super(context, "miduo.db", null, 2);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(TABLE_CREATE_SQL[0]);
		db.execSQL(TABLE_CREATE_SQL[1]);
	}

	// 更新表
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TB_NAME[0]);
		db.execSQL("DROP TABLE IF EXISTS " + TB_NAME[1]);
		onCreate(db);
		Log.e("Database", "onUpgrade");
	}

	// 更新列
	public void updateColumn(SQLiteDatabase db, String oldColumn,
			String newColumn, String typeColumn) {
		try {
			db.execSQL("ALTER TABLE " + TB_NAME[0] + " CHANGE " + oldColumn + " "
					+ newColumn + " " + typeColumn);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
